<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品名称">
              <a-input placeholder="请输入商品名称" v-model="queryParam.title"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24" v-if="shopType==1">
            <a-form-item label="店铺名称">
              <a-input placeholder="请输入店铺名称" v-model="queryParam.shopName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="商品货号">
              <a-input placeholder="请输入商品货号" v-model="queryParam.code"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="活动类型">
              <j-dict-select-tag placeholder="全部" v-model="queryParam.itemType" dictCode="market_type"/>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-radio-group :default-value="null" v-model="queryParam.activeStatus" @change="searchQuery" button-style="solid">
        <a-radio-button :value="null">
          全部
        </a-radio-button>
        <a-radio-button :value="2">
          活动中
        </a-radio-button>
        <a-radio-button :value="1">
          正常
        </a-radio-button>
        <a-radio-button :value="0">
          下架中
        </a-radio-button>
      </a-radio-group>
      <a-divider v-if="shopType==3" type="vertical"/>
      <a-button v-if="shopType==3" @click="handleAdd2" type="primary" icon="plus">添加秒杀商品</a-button>
      <a-divider v-if="shopType==3" type="vertical"/>
      <a-button v-if="shopType==3" @click="handleAdd3" type="primary" icon="plus">添加拼团商品</a-button>
      <a-divider v-if="shopType==3" type="vertical"/>
      <a-button v-if="shopType==3" @click="handleAdd5" type="primary" icon="plus">添加预售商品</a-button>

    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        <a-divider type="vertical" v-if="selectedRowKeys.length > 0&&queryParam.activeStatus==0"/>
        <a style="margin-left: 24px" v-if="selectedRowKeys.length > 0&&queryParam.activeStatus==0"
           @click="batchUp">批量上架</a>
        <a-divider type="vertical" v-if="selectedRowKeys.length > 0&&queryParam.activeStatus==1"/>
        <a style="margin-left: 24px" v-if="selectedRowKeys.length > 0&&queryParam.activeStatus==1" @click="batchDown">批量下架</a>
      </div>

      <a-table ref="table" size="middle" :scroll="{x:true}" bordered rowKey="id" :columns="columns"
               :dataSource="dataSource" :pagination="ipagination"
               :loading="loading" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
               class="j-table-force-wrap"
               @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" @click="openUrlView(text)" height="80px" alt=""
               style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>

        <template slot="libStockSlot" slot-scope="text, record">
          <div v-if="record.itemType==2">{{ record.msStock }}</div>
          <div v-if="record.itemType==3">{{ record.ptStock }}</div>
          <div v-if="record.itemType==5">{{ record.yyStock }}</div>
        </template>

        <template slot="activeStatus" slot-scope="text, record">
          <a-tag color="cyan" v-if="record.activeStatus==0">
            下架中
          </a-tag>
          <a-tag color="green" v-if="record.activeStatus==1">
            正常
          </a-tag>
          <a-tag color="purple" v-if="record.activeStatus==2">
            活动中
          </a-tag>
        </template>

        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">查看</a>
          <br/>

          <a v-if="record.activeStatus!=2&&shopType==3" @click="handleEdit(record)">编辑</a>
          <br v-if="record.activeStatus!=2&&shopType==3"/>

          <a-popconfirm v-if="record.activeStatus==0&&shopType==3" title="确定上架吗?" @confirm="() => handleUp(record)">
            <a>上架</a>
          </a-popconfirm>
          <br v-if="record.activeStatus==0&&shopType==3"/>

          <a-popconfirm v-if="record.activeStatus==1&&shopType==3" title="确定下架吗?" @confirm="() => handleDown(record)">
            <a>下架</a>
          </a-popconfirm>
          <br v-if="record.activeStatus==1&&shopType==3"/>

          <a @click="handleList(record)">活动记录</a>

          <br v-if="record.activeStatus!=2&&shopType==3"/>
          <a-popconfirm v-if="record.activeStatus!=2&&shopType==3" title="确定删除吗?"
                        @confirm="() => handleDelete(record.id)">
            <a>删除</a>
          </a-popconfirm>

        </span>

      </a-table>
    </div>
    <msms-good-ref ref="gmsMsGoodRef" @ok="modalFormOk"></msms-good-ref>
    <ptms-good-ref ref="gmsPtGoodRef" @ok="modalFormOk"></ptms-good-ref>
    <yyms-good-ref ref="gmsYyGoodRef" @ok="modalFormOk"></yyms-good-ref>
    <gms-good-lib-modal ref="modalForm" @ok="modalFormOk"></gms-good-lib-modal>
  </a-card>
</template>

<script>
import {deleteAction, getAction} from '@/api/manage'
import '@/assets/less/TableExpand.less'
import {mixinDevice} from '@/utils/mixin'
import {JeecgListMixin} from '@/mixins/JeecgListMixin'
import GmsGoodLibModal from './modules/GmsGoodLibModal'
import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
import MsmsGoodRef from '@/views/gospel/mallPlus/ms/modules/MsmsGoodRef'
import PtmsGoodRef from '@/views/gospel/mallPlus/pt/modules/PtmsGoodRef'
import YymsGoodRef from '@/views/gospel/mallPlus/yy/modules/YymsGoodRef'

export default {
  name: 'GmsGoodLibList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    JDictSelectTag,
    GmsGoodLibModal,
    MsmsGoodRef,
    PtmsGoodRef,
    YymsGoodRef
  },
  data() {
    return {
      description: '活动商品库管理页面',
      // 表头
      columns: [
        {
          title: '商品货号',
          align: "center",
          dataIndex: 'code'
        },
        {
          title: '商品名称',
          align: "center",
          dataIndex: 'title',
          width: 250,
        },
        {
          title: '图片',
          align: "center",
          dataIndex: 'imgUrl',
          scopedSlots: {customRender: "imgSlot"}
        },
        {
          title: '所属店铺',
          align: "center",
          dataIndex: 'shopName'
        },
        {
          title: '商品类型',
          align: "center",
          dataIndex: 'itemType_dictText'
        },
        {
          title: '活动库存',
          align: "center",
          dataIndex: 'yyStock',
          scopedSlots: {customRender: "libStockSlot"}
        },
        {
          title: '活动状态',
          align: "center",
          dataIndex: 'activeStatus',
          scopedSlots: {customRender: 'activeStatus'}
        },
        {
          title: '创建时间',
          align: "center",
          dataIndex: 'createTime',
          sorter: true,
          width: 100,
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: "center",
          fixed: "right",
          width: 147,
          scopedSlots: {customRender: 'action'}
        }
      ],
      url: {
        list: "/mallPlus/gmsGoodLib/list",
        delete: "/mallPlus/gmsGoodLib/delete",
        deleteBatch: "/mallPlus/gmsGoodLib/deleteBatch",
        upBatch: "/mallPlus/gmsGoodLib/upBatch",
        downBatch: "/mallPlus/gmsGoodLib/downBatch",
        exportXlsUrl: "/mallPlus/gmsGoodLib/exportXls",
        importExcelUrl: "mallPlus/gmsGoodLib/importExcel",

      },
      dictOptions: {},
    }
  },
  created() {
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    handleAdd2: function () {
      this.$refs.modalForm.add(2)
      this.$refs.modalForm.title = "新增秒杀商品"
      this.$refs.modalForm.disableSubmit = false
    },
    handleAdd3: function () {
      this.$refs.modalForm.add(3)
      this.$refs.modalForm.title = "新增拼团商品"
      this.$refs.modalForm.disableSubmit = false
    },
    handleAdd5: function () {
      this.$refs.modalForm.add(5)
      this.$refs.modalForm.title = "新增预约商品"
      this.$refs.modalForm.disableSubmit = false
    },
    handleUp(record) {
      var that = this
      getAction(that.url.upBatch, {ids: record.id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    handleDown(record) {
      var that = this
      getAction(that.url.downBatch, {ids: record.id}).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    batchUp() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ""
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ","
        }
        var that = this
        this.$confirm({
          title: "确认上架",
          content: "是否上架选中数据?",
          onOk: function () {
            that.loading = true
            getAction(that.url.upBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    },
    batchDown() {
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ""
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ","
        }
        var that = this
        this.$confirm({
          title: "确认下架",
          content: "是否下架选中数据?",
          onOk: function () {
            that.loading = true
            getAction(that.url.downBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            }).finally(() => {
              that.loading = false
            })
          }
        })
      }
    },
    handleList(record) {
      let self = this
      if (record.itemType == 2) {
        self.$refs.gmsMsGoodRef.open(record.id)
        self.$refs.gmsMsGoodRef.visible = true
      } else if (record.itemType == 3) {
        self.$refs.gmsPtGoodRef.open(record.id)
        self.$refs.gmsPtGoodRef.visible = true
      } else if (record.itemType == 5) {
        self.$refs.gmsYyGoodRef.open(record.id)
        self.$refs.gmsYyGoodRef.visible = true
      }
    },
    initDictConfig() {
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
